/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.gui;

import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import model.dao.DaoProfessor;
import model.dominio.Professor;
import model.dominio.Turma;
/**
 *
 * @author Teao
 */
public class GuiProfConsultar extends javax.swing.JPanel {
    
    private List<Professor> listaProfessor;
    private List<Turma> listaTurma;
    private Professor professor = new Professor();

    public GuiProfConsultar() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        SelBusca = new javax.swing.ButtonGroup();
        jDialog1 = new javax.swing.JDialog();
        jDialog2 = new javax.swing.JDialog();
        buscarProfessor = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabProfessor = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        quantDisc = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        nome = new javax.swing.JTextField();
        cpf = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        comboProfessor = new javax.swing.JComboBox();
        salvar = new javax.swing.JButton();
        removerProfessor = new javax.swing.JButton();
        removerDados = new javax.swing.JButton();

        setLayout(null);

        buscarProfessor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/gui/icones/buscar.png"))); // NOI18N
        buscarProfessor.setText("BUSCAR");
        buscarProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarProfessorActionPerformed(evt);
            }
        });
        add(buscarProfessor);
        buscarProfessor.setBounds(540, 30, 120, 40);

        tabProfessor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "", "Disciplina", "Ano", "Período"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tabProfessor.setToolTipText("");
        jScrollPane1.setViewportView(tabProfessor);

        add(jScrollPane1);
        jScrollPane1.setBounds(10, 150, 660, 190);

        jLabel1.setText("Selecione o metodo de busca:");
        add(jLabel1);
        jLabel1.setBounds(10, 10, 180, 20);

        jLabel2.setText("O professor");
        add(jLabel2);
        jLabel2.setBounds(40, 100, 70, 30);

        jLabel3.setText("lecionou ");
        add(jLabel3);
        jLabel3.setBounds(440, 100, 50, 30);

        quantDisc.setEditable(false);
        quantDisc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quantDiscActionPerformed(evt);
            }
        });
        add(quantDisc);
        quantDisc.setBounds(500, 100, 60, 30);

        jLabel4.setText("disciplinas:");
        add(jLabel4);
        jLabel4.setBounds(570, 100, 70, 30);

        jLabel5.setText("CPF:");
        add(jLabel5);
        jLabel5.setBounds(280, 40, 50, 30);

        nome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomeActionPerformed(evt);
            }
        });
        add(nome);
        nome.setBounds(50, 40, 210, 30);

        try {
            cpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        cpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpfActionPerformed(evt);
            }
        });
        add(cpf);
        cpf.setBounds(330, 40, 140, 30);

        jLabel6.setText("Nome:");
        add(jLabel6);
        jLabel6.setBounds(10, 40, 50, 30);

        comboProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboProfessorActionPerformed(evt);
            }
        });
        add(comboProfessor);
        comboProfessor.setBounds(110, 100, 310, 30);

        salvar.setText("Salvar");
        salvar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarActionPerformed(evt);
            }
        });
        add(salvar);
        salvar.setBounds(500, 350, 170, 50);

        removerProfessor.setText("Remover Professor");
        removerProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removerProfessorActionPerformed(evt);
            }
        });
        add(removerProfessor);
        removerProfessor.setBounds(10, 350, 150, 50);

        removerDados.setText("Remover dados");
        removerDados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removerDadosActionPerformed(evt);
            }
        });
        add(removerDados);
        removerDados.setBounds(180, 350, 130, 50);
    }// </editor-fold>//GEN-END:initComponents

    private void buscarProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarProfessorActionPerformed
        // TODO add your handling code here:
        buscarProfessor();
    }//GEN-LAST:event_buscarProfessorActionPerformed

    private void quantDiscActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quantDiscActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_quantDiscActionPerformed

    private void nomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nomeActionPerformed

    private void cpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cpfActionPerformed

    private void comboProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboProfessorActionPerformed
        professor = listaProfessor.get(comboProfessor.getSelectedIndex());
        preencheTabela(professor);
        
    }//GEN-LAST:event_comboProfessorActionPerformed

    private void salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarActionPerformed
        // Ao clicar nesse botão, irá para uma caixa de dialogo
        // questionando o usuário se aceita excluir o dado em questão.
        // Se sim, gera uma outra caixa dizendo que o dado foi
        // excluido com sucesso, e exclui o(s) dado(s) também.

        // Se não, volta para o programa principal.

        // Ele irá remover o(s) professor(s) selecionado(s) na tabela.

        Professor professor = new Professor();
        if (nome.getText().isEmpty() || cpf.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Digite os dados corretamente", "ERRO", JOptionPane.ERROR_MESSAGE);
        } else {
            professor.setNome(nome.getText());
            professor.setCpf(cpf.getText());
            DaoProfessor dao = new DaoProfessor();
            professor = (Professor) dao.salvar(professor);
            if (professor != null) {
                JOptionPane.showMessageDialog(this, "Dados registrados com sucesso", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
                this.setVisible(false);
            }

        }
    }//GEN-LAST:event_salvarActionPerformed

    private void removerProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removerProfessorActionPerformed

        DaoProfessor dao = new DaoProfessor();
        if (dao.excluir(professor)) {
            JOptionPane.showMessageDialog(this, "Dados excluidos com sucesso", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
            this.setVisible(false);
        }
    }//GEN-LAST:event_removerProfessorActionPerformed

    private void removerDadosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removerDadosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_removerDadosActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup SelBusca;
    private javax.swing.JButton buscarProfessor;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox comboProfessor;
    private javax.swing.JFormattedTextField cpf;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nome;
    private javax.swing.JTextField quantDisc;
    private javax.swing.JButton removerDados;
    private javax.swing.JButton removerProfessor;
    private javax.swing.JButton salvar;
    private javax.swing.JTable tabProfessor;
    // End of variables declaration//GEN-END:variables

    public void buscarProfessor() {
        Professor a = new Professor();
        a.setNome(nome.getText());
        a.setCpf(cpf.getText());
        comboProfessor.removeAllItems();
        listaProfessor = new DaoProfessor().buscarProfessor(a);
        for (Professor professor : listaProfessor) {
            comboProfessor.addItem(professor.getNome() + " - " + professor.getCpf());
        }
        if (listaProfessor.size() == 1) {
            preencheTabela(listaProfessor.get(0));
        }
    }

    public void preencheTabela(Professor professor) {
//        List<Turma> turmas = professor.getTurma();
//        DefaultTableModel model = (DefaultTableModel) tabProfessor.getModel();
//        String[] linha =new String[3];
//        for (Turma t : turmas) {
//            linha[0] = t.getDisciplina().getNome();
//            linha[1] = Integer.toString(t.getAno());
//            linha[2] = Integer.toString(t.getPeriodo());
//            model.addRow(linha);
//            
//        }
    }

}
